package com.ruoyi.framework.storage;

import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.utils.file.FileUtils;
import io.minio.*;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;

/**
 * @author Yogeek
 * @date 2018/7/16 16:10
 * @decrpt 阿里云对象存储服务
 */
@Slf4j
@Setter
public class MinioStorage implements Storage {
    private String prefixUrl;
    private String endpoint;
    private String bucketName;

    private MinioClient minioClient = null;

    @Override
    public String getStoragePath() {
        return endpoint;
    }

    private String getBaseUrl() {
        return prefixUrl + bucketName + "/";
    }

    @Override
    public String store(InputStream inputStream, String keyName) {
        return this.store(inputStream, keyName, "application/octet-stream");
    }

    @Override
    public String store(InputStream inputStream, String keyName, String contentType) {
        String fileUrl = "";
        try {
            // 检查存储桶是否已经存在
            if (minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
                log.info("Bucket already exists.");
            } else {
                // 创建一个名为ota的存储桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                log.info("create a new bucket.");
            }

            // 使用putObject上传一个本地文件到存储桶中。
            PutObjectArgs objectArgs = PutObjectArgs.builder().object(keyName)
                    .bucket(bucketName)
                    .contentType(contentType)
                    .stream(inputStream, inputStream.available(), -1).build();
            ObjectWriteResponse objectWriteResponse = minioClient.putObject(objectArgs);
            log.info("store result:{}", objectWriteResponse);

            inputStream.close();
            fileUrl = prefixUrl + bucketName + "/" + keyName;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return fileUrl;
    }

    @Override
    public Path load(String keyName) {
        return null;
    }

    @Override
    public Resource loadAsResource(String keyName) {
        try {
            URL url = new URL(this.generateUrl(keyName));
            Resource resource = new UrlResource(url);
            if (resource.exists() || resource.isReadable()) {
                return resource;
            } else {
                return null;
            }
        } catch (MalformedURLException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public void delete(String keyName) {
        try {
            RemoveObjectArgs objectArgs = RemoveObjectArgs.builder().object(keyName)
                    .bucket(bucketName).build();
            minioClient.removeObject(objectArgs);
        } catch (Exception e) {
            log.info("文件删除失败" + e.getMessage());
        }
    }

    @Override
    public String generateUrl(String keyName) {
        return getBaseUrl() + keyName;
    }
}
